// mock.js
import Mock from 'mockjs'
import data from './data'

import config from '../config'

import qs from 'qs'

function getParam(paramName, url, defaults){
    const arrObj = url.split('?')
    if(arrObj.length > 1){
        const arrPara = arrObj[1].split('&')
        let arr
        for (let i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split('=')
            if(arr != null && arr[0] === paramName){
                return arr[1]
            }
        }
    }
    return defaults
}

/* 
    home
*/
// home 页面数据
Mock.mock(config.baseURL+'api/data/home', 'get', data.homeData)

/* 
    user
*/
// 搜索用户信息
Mock.mock(RegExp(`${config.baseURL}api/data/user/search` + ".*"), 'get', (options)=>{
    const req = qs.parse(options.url.split('?')[1])

    const newdata = data.userData.data.filter((item, index) => {
        return RegExp(req.value).test(item[req.key])
    });

    const page = req.page*1
    const pageSize = req.pageSize*1

    let start = (page - 1) * pageSize
    if(start > 0) start - 1
    let end = start + pageSize

    return {
        data: newdata.slice(start, end),
        total: newdata.length
    }
})
// 用户数据
Mock.mock(RegExp(`${config.baseURL}api/data/user` + ".*"), 'get', (options)=>{
    const page = getParam('page', options.url, 1)*1
    const pageSize = getParam('pageSize', options.url, 14)*1

    let start = (page - 1) * pageSize
    if(start > 0) start - 1
    let end = start + pageSize

    return {
        data: data.userData.data.slice(start, end),
        total: data.userData.data.length
    }
})
// 新增用户信息
Mock.mock(`${config.baseURL}api/data/user`, 'post', (options)=>{
    const req = qs.parse(options.body)
    const Template = Mock.mock({
        id: '@guid()', // 用户id
        username: "@word(4,12)", // 用户名
        nickname: '@clast(1,12)', // 随机昵称
        name: '@cname', // 姓名
        phone: /^1[34578]\d{9}$/, //电话号码
        email:'@email',//邮箱
        sex(){ // 用户性别
            return Mock.Random.boolean() ? '男' : '女'
        },
        address: '@county(true)', //地址
        createTime: '@datetime', //注册时间
        info() { // 用户简介
            return Mock.Random.cparagraph()
        },
        avatar() { //用户头像
            return Mock.Random.image('100×100', Mock.Random.color(), '#FFF', this.nickname).replace(
                '×',
                'x'
            )
        },
        'state|0-1':1, // 用户状态
    })

    for (const key in req) {
        if (Template[key]) {
            Template[key] = req[key]
        }
    }

    data.userData.data.unshift(Template)

    return {
        massage:'ok'
    }
})
// 更改用户信息
Mock.mock(RegExp(`${config.baseURL}api/data/user` + ".*"), 'put', (options)=>{
    const req = qs.parse(options.url.split('?')[1])

    data.userData.data.forEach((item, index) => {
        if(item.id === req.id){
            data.userData.data[index] = req
        }
    });

    return {
        massage:'ok'
    }
})
// 删除用户信息
Mock.mock(RegExp(`${config.baseURL}api/data/user` + ".*"), 'delete', (options)=>{
    const req = qs.parse(options.url.split('?')[1])

    data.userData.data.forEach((item, index) => {
        if(item.id === req.id){
            data.userData.data.splice(index, 1)
        }
    });

    return {
        massage:'ok'
    }
})
// 删除用户信息
Mock.mock(`${config.baseURL}api/login`, 'post', (options)=>{
    const req = qs.parse(options.body)
    if(req.username === 'admin' && req.password === '123456'){
        return {
            message:'ok',
            code: 200,
            token: 'gyaa8hihq8c9h.ahc89oacbau9.9wnvuppsodjs',
            Grade:'admin'
        }
    }else if(req.username === 'editor' && req.password === '123456'){
        return {
            message:'ok',
            code: 200,
            token: 'sa654f6s64g65s4.a3gfa4gf4ea653.42c437g6878g',
            Grade:'editor'
        }
    }{
        return {
            message:'用户名或密码不正确',
            code: 401,
        }
    }
})
